import sqlite3

# 检查数据库中的交易记录
def check_trade_records():
    try:
        # 连接到SQLite数据库
        conn = sqlite3.connect('trade_monitor.db')
        cursor = conn.cursor()
        
        # 查询trade_records表中的记录数量
        cursor.execute("SELECT COUNT(*) FROM trade_records")
        count = cursor.fetchone()[0]
        print(f"trade_records表中的记录数量: {count}")
        
        # 如果有记录，显示最近的10条记录
        if count > 0:
            print("\n最近的10条交易记录:")
            cursor.execute("SELECT * FROM trade_records ORDER BY timestamp DESC LIMIT 10")
            records = cursor.fetchall()
            
            # 获取表结构信息
            cursor.execute("PRAGMA table_info(trade_records)")
            columns = [col[1] for col in cursor.fetchall()]
            
            # 打印表头
            print(" | ".join(columns))
            print("-" * 100)
            
            # 打印记录
            for record in records:
                print(" | ".join(str(field) for field in record))
        
    except Exception as e:
        print(f"查询数据库时出错: {str(e)}")
    finally:
        # 关闭连接
        if conn:
            conn.close()

if __name__ == "__main__":
    check_trade_records()